package bb.lanxing.adapter.discover;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import bb.lanxing.R;
import bb.lanxing.model.database.BbsType;

public class BbsReportAdapter extends RecyclerView.Adapter<BbsReportAdapter.ViewHolder> {
    private List<BbsType> list = new ArrayList<>();
    private OnItemClickListener listener;
    private Context mContext;

    public int bbsReportType = 0;
    public boolean isSelected = false;

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView name;
        ImageView pass;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
            pass = itemView.findViewById(R.id.pass);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_bbs_report, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.name.setText(list.get(position).typeName);
        if (isSelected && bbsReportType == position) {
            holder.pass.setVisibility(View.VISIBLE);
        } else {
            holder.pass.setVisibility(View.GONE);
        }
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(position);
            }
            isSelected = true;
            notifyDataSetChanged();
        });
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void updateData(List<BbsType> dataList) {
        list = dataList;
        notifyDataSetChanged();
    }
}